const { upgrades,ethers } = require("hardhat")
const {expect} = require("chai");

async function  main() {

    const [signer, buyer] = await ethers.getSigners()

    // 获取eth moke 合约
    const mockV3Aggregator = await ethers.getContractFactory("MockV3Aggregator")
    const mokeAddress = await mockV3Aggregator.deploy(0, 3000)
    let ehtPrice = await mokeAddress.waitForDeployment();
    let ehtPriceAddress =  await ehtPrice.getAddress();

    const NFTMarket = await ethers.getContractFactory("NFTUUDSMarket")

    const box = await upgrades.deployProxy(NFTMarket, [ehtPriceAddress]);
    await box.waitForDeployment();
    let boxaddress = await box.getAddress();
    console.log("Box deployed to:", boxaddress);
    const [firstAccount,secondAccount,threeAccount] = await ethers.getSigners()

    //部署ERC20
    const usdc = await ethers.getContractFactory("MockUSDC")
    const usdcDeploy = await usdc.deploy()
     await usdcDeploy.waitForDeployment();
    const usdcDeployAddress = await usdcDeploy.getAddress();

    //部署ERC20价格
    const moke20Address = await mockV3Aggregator.deploy(0, 1000)
    let Erc20Price = await moke20Address.waitForDeployment();
    let Erc20PriceAddress =  await Erc20Price.getAddress();

    await box.setPriceFeeds(usdcDeployAddress,Erc20PriceAddress);

    console.log("eth--price--MockV3Aggregator", await box.priceFeeds(ethers.ZeroAddress))
    console.log("erc20--price--MockV3Aggregator", await box.priceFeeds(Erc20PriceAddress))

    // 创建NFT
    const nft = await ethers.getContractFactory("Task02NFT")
    const nftDeploy = await nft.deploy("CompanyNFt", "CNF");
    await nftDeploy.waitForDeployment();
    const nftDeployaddress = nftDeploy.getAddress();

    for (let i= 0; i<5;i++) {
       await nftDeploy.awardItem(firstAccount, "1" + i);
    }
    const tokenId = 1;
    // 授权nft
    await nftDeploy.connect(firstAccount).approve(boxaddress,tokenId);
    //创建拍卖
   await box.createAuction(20,1,nftDeployaddress,tokenId);
   console.log( "create auction success",await box.auctions(0))
    // 测试第二个账户TOKEN竞拍
    //铸造USDC
   await usdcDeploy.mint(secondAccount, 5);
    //开始竞拍
    await usdcDeploy.connect(secondAccount).approve(boxaddress,4);
    await box.connect(secondAccount).placeBid(0,4,usdcDeployAddress);
    console.log( "second placeBid success",)
    expect((await box.auctions(0)).highestBidder).to.equal(secondAccount.address);
    console.log("secondAccount balance =", await usdcDeploy.balanceOf(secondAccount))
    // 测试第三个账户ETH竞拍
   await box.connect(threeAccount).placeBid(0,0,ethers.ZeroAddress,{value: ethers.parseEther("2")})
    expect((await box.auctions(0)).highestBidder).to.equal(threeAccount.address);
    console.log("secondAccount balance =", await usdcDeploy.balanceOf(secondAccount))
    const provider = ethers.provider;
    console.log("threeAccount balance =", ethers.formatEther(await  provider.getBalance(threeAccount)))
    //二号重新TOKEN竞拍
    await usdcDeploy.mint(secondAccount, 2);
    await usdcDeploy.connect(secondAccount).approve(boxaddress,7);
    await box.connect(secondAccount).placeBid(0,7,usdcDeployAddress);
    expect((await box.auctions(0)).highestBidder).to.equal(secondAccount);
    console.log("secondAccount balance =", await usdcDeploy.balanceOf(secondAccount))
    console.log("threeAccount balance =", ethers.formatEther(await  provider.getBalance(threeAccount)))

    //竞拍结束
   await box.endAuction(0);
    expect((await nftDeploy.ownerOf(tokenId))).to.equal(secondAccount);
    expect((await usdcDeploy.balanceOf(firstAccount))).to.equal(7);























    // // 升级逻辑
    // const [firstAccount] = await ethers.getSigners()
    // const BoxV2 = await ethers.getContractFactory("NFTMarketV2");
    // const box2 = await upgrades.upgradeProxy(boxaddress, BoxV2);
    // await box2.waitForDeployment()
}


main().then().catch(err => {
    console.error(err)
    process.exit(1)
})